package com.qf.business.system.controller;

import com.qf.business.system.service.PowerService;
import com.qf.business.system.service.RolePowerTableService;
import com.qf.data.base.r.R;
import com.qf.data.base.r.Rs;
import com.qf.data.base.utils.QfBeanUtils;
import com.qf.data.system.dto.PowerDto;
import com.qf.data.system.entity.Power;
import com.qf.data.system.entity.RolePowerTable;
import com.qf.data.system.vo.input.PowerInput;
import com.qf.data.system.vo.output.PowerOutput;
import com.qf.data.system.vo.output.PowerSimpleOutput;
import com.qf.data.system.vo.output.PowerTreeNodeOutput;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * description:
 * author: Ken
 * 公众号：Java架构栈
 */
@RestController
@RequestMapping("/power")
public class PowerController {

    @Autowired
    private PowerService powerService;

    @Autowired
    private RolePowerTableService rolePowerTableService;

    /**
     * 查询权限列表
     * @return
     */
    @RequestMapping("/list")
    public R list(){
        List<PowerDto> powerDtos = powerService.queryList();
        List<PowerOutput> powerOutputs = QfBeanUtils.listCopy(powerDtos, PowerOutput.class);
        return Rs.create(powerOutputs);
    }

    /**
     * 新增时的查询
     * @return
     */
    @RequestMapping("/listByInsert")
    public R listByInsert(){
        List<PowerDto> powerDtos = powerService.queryList();
        List<PowerSimpleOutput> powerOutputs = QfBeanUtils.listCopy(powerDtos, PowerSimpleOutput.class);
        return Rs.create(powerOutputs);
    }

    /**
     * 权限列表 - 树状结构
     * List<Power> -
     * Power(1,null,名称.....)
     * Power(2,1,名称.....)
     * Power(3,1,名称.....)
     * Power(4,2,名称.....)
     * Power(id,pid,名称.....)
     *
     *
     * --->转换
     * TreeNode(label-权限名称, children - [TreeNode(lobel-子权限名称, children - []), ])
     *
     *
     * List<PowerTreeNodeOutput>
     *     - PowerTreeNodeOutput1
     *     - PowerTreeNodeOutput3
     *
     * Map<key, value>
     *     1, PowerTreeNodeOutput1 - children:[PowerTreeNodeOutput2, PowerTreeNodeOutput3]
     *     2, PowerTreeNodeOutput2 - children:[PowerTreeNodeOutput4]
     *     3, PowerTreeNodeOutput3 - children:[]
     *     4, PowerTreeNodeOutput4
     *
     * @return
     */
    @RequestMapping("/listByTree")
    public R listByTree(){

        List<PowerTreeNodeOutput> rootTreeNode = new ArrayList<>();
        Map<Long, PowerTreeNodeOutput> treeNodeMap = new HashMap<>();

        //查询出所有的权限列表
        List<Power> powers = powerService.query().orderByAsc("pid").list();
//        for (Power power : powers) {
//            System.out.println("权限树的查询结果：" + power);
//        }

        for (Power power : powers) {
            //将power 转换成 TreeNodeOutput
            PowerTreeNodeOutput treeNode = new PowerTreeNodeOutput()
                    .setId(power.getId())
                    .setLabel(power.getPowerName())
                    .setType(power.getType());

            if (power.getPid() != null) {
                //不是顶级节点，从Map集合中查询到父级节点
                PowerTreeNodeOutput parentTreeNode = treeNodeMap.get(power.getPid());
                parentTreeNode.addChildren(treeNode);
            } else {
                //是顶级节点
                rootTreeNode.add(treeNode);
            }
            //将当前节点放入Map集合中
            treeNodeMap.put(treeNode.getId(), treeNode);
        }
        //返回权限树
        return Rs.create(rootTreeNode);
    }

    /**
     * 根据角色的id查询拥有的权限（id 集合）
     * @param rid
     * @return
     */
    @RequestMapping("/rolePower")
    public R listByRid(Long rid){
        List<RolePowerTable> rolePowerTables = rolePowerTableService.queryByRid(rid);
        //当前角色拥有权限的id集合
        List<Long> pids = rolePowerTables.stream()
                .map(rolePowerTable -> rolePowerTable.getPid())
                .collect(Collectors.toList());
        return Rs.create(pids);
    }

    /**
     * 新增权限
     * @param input
     * @return
     */
    @RequestMapping("/insert")
    public R insert(@Validated PowerInput input){
        Power power = QfBeanUtils.beanCopy(input, Power.class);
        powerService.save(power);
        return Rs.create("succ");
    }
}
